<template>
  <div>
    <tableComponents
      :pagination="{currentPage,pageSize:pagesize,total:cate_list.all_num}"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
      :isHaveExport="false"
      @toAdd="add_cate"
      :isShow="false"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="cate_list.data"
          v-loading="loading"
          stripe
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.8%"
        >
          <el-table-column prop="sort" sortable :sort-method="sorta" label="排序" min-width="80">
            <template slot-scope="scope">
              <el-input
                @change="sortNew(scope.$index, scope.row)"
                v-model.number="scope.row.sort"
                size="small"
              ></el-input>
            </template>
          </el-table-column>

          <el-table-column label="类别名称" min-width="250">
            <template slot-scope="scope">{{scope.row.title}}</template>
          </el-table-column>

          <el-table-column label="是否显示" min-width="130" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-switch
                class="zdySwith"
                :width="82"
                v-model="scope.row.show"
                active-color="#13ce66"
                inactive-color="#ffffff"
                active-text="显示"
                inactive-text="不显示"
                active-value="1"
                @change="getIskeck($event, scope.row)"
                inactive-value="0"
              ></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="创建时间" min-width="80">
            <template slot-scope="scope">{{scope.row.created_time | getTimeFormat}}</template>
          </el-table-column>

          <el-table-column label="操作" min-width="150">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="primary"
                @click="add_cate(scope.row)"
              >编辑</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="danger"
                @click="delete_cate(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>

    <el-dialog
      :close-on-click-modal="false"
      :title="add_form.id?'编辑':'添加'"
      class="tl size_13"
      width="800px"
      :visible.sync="dialogaddVisible"
    >
      <el-form :model="add_form" ref="form" class="tl size_13" :label-width="formLabelWidth">
        <el-form-item label="分类名称：">
          <el-col :span="12">
            <el-input
              type="text"
              :rows="6"
              clearable
              placeholder="请输入"
              v-model="add_form.title"
              autocomplete="off"
            ></el-input>
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button :loading="$store.state.isLoading" type="primary" @click="subInfo()">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "cate",
  data() {
    return {
      add_form: {
        title: ""
      },
      dialogaddVisible: false,
      cate_list: {},
      loading: false,
      currentPage: 1, //初始页
      pagesize: 10, //    每页的数据
      formLabelWidth: "120px"
    };
  },
  components: {},
  created() {
    this.get_cate_list();
  },
  methods: {
    getIskeck(val, row) {
      var params = {};
      params.id = row.id;
      params.show = val;
      this.$api.getShops.integral_cate_show(params).then(res => {
        if (res.data.code == 200 && res.data.result) {
          this.get_cate_list();
        } else {
          this.get_cate_list();
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
      });
    },
    sortNew(index, row) {
      var params = {};
      params.id = row.id;
      params.sort = row.sort;
      this.$api.getShops.integral_cate_sort(params).then(res => {
        if (res.data.code == 200 && res.data.result) {
          this.get_cate_list();
        } else {
          this.get_cate_list();
          this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
        }
      });
    },
    add_cate(val) {
      if (val) {
        this.add_form = val;
      } else {
        this.add_form = {
          title: ""
        };
      }
      this.dialogaddVisible = true;
    },
    subInfo() {
      if (this.add_form.id) {
        this.$api.getShops.integral_cate_saveedit(this.add_form).then(res => {
          if (res.data.code == 200) {
            this.$fnc.alertSuccess("修改成功");
            this.dialogaddVisible = false;
            this.get_cate_list();
          } else {
            this.$fnc.alertError(res.data.result);
            this.get_cate_list();
          }
        });
      } else {
        this.$api.getShops.integral_cate_saveadd(this.add_form).then(res => {
          if (res.data.code == 200) {
            this.$fnc.alertSuccess("添加成功");
            this.dialogaddVisible = false;
            this.get_cate_list();
          } else {
            this.$fnc.alertError(res.data.result);
            this.get_cate_list();
          }
        });
      }
    },
    delete_cate(id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          this.$api.getShops.integral_cate_del({ id: id }).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.get_cate_list();
            } else {
              this.$swal("删除失败！", res.data.result, "error");
              this.get_cate_list();
            }
          });
        }
      });
    },
    get_cate_list(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;
      this.$api.getShops.integral_cate_lists(params).then(res => {
        if (res.data.code == 200) {
          this.cate_list = res.data.result;
          this.loading = false;
        } else {
          this.$fnc.alertError(res.data.result);
          this.loading = false;
        }
      });
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function(size) {
      //每页下拉显示数据
      var params = {};
      params.page = "1";
      params.page_size = size;
      this.get_cate_list(params);
      this.pageSize = size;
    },
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage;
      var params = {};
      params.page = currentPage;
      params.page_size = this.pageSize;
      this.get_cate_list(params);
    }
  }
};
</script>
<style scoped>
.vide0-con {
  background-color: transparent !important;
  border: none;
  box-shadow: none;
  width: 600px;
}
.vide0-con .el-dialog__header {
  display: none;
}
</style>
